package com.xbongbong.paas.connection.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.connection.enums.OpenAppEnum;
import com.xbongbong.paas.connection.service.IOpenAppService;
import com.xbongbong.paas.connection.service.OpenAppFactory;
import com.xbongbong.paas.connection.service.imp.EsignForConnectionService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.ThirdPartyUsageErrorCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author chy
 * @ProjectName xbb-connection
 * @Description: 连接相关共用对外接口
 * @date 2019/1/24 15:34
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/connection")
public class ConnectionController {
    private static final Logger LOG = LoggerFactory.getLogger(ConnectionController.class);
    @Resource
    private OpenAppFactory openAppFactory;
    @Resource
    private EsignForConnectionService esignForConnectionService;

    /**
     * 数据初始化
     * @param jsonObject
     * @param br
     * @return
     * 创建时间 2019/3/19 4:47 PM
     * 修改时间 2019/3/19 4:47 PM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/callback", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
        public String callback(@RequestBody @Valid JSONObject jsonObject, BindingResult br) {
        XbbResponse<BaseVO> response;

        try {
            Integer openAppId = jsonObject.getInteger("openAppId");
            OpenAppEnum openAppEnum = OpenAppEnum.getByOpenAppId(openAppId);

            if (Objects.isNull(openAppEnum)) {
                throw new XbbException(ThirdPartyUsageErrorCodeEnum.API_ERROR_900005, String.format(ThirdPartyUsageErrorCodeEnum.API_ERROR_900005.getMsg(), "openAppId"));
            }

            IOpenAppService iOpenAppService = openAppFactory.openAppFactory(openAppEnum);
            iOpenAppService.openAppApiCallbackAccept(jsonObject);
            response = new XbbResponse<>(new BaseVO());
        } catch (XbbException e) {
            e.printStackTrace();
            response = new XbbResponse<>(e);
        }
        return JSON.toJSONString(response);
    }


    /**
     * e签宝回调(签署状态变动/授权回调)
     * @param jsonObject
     * @param br
     * @return
     * 创建时间 2019/3/19 4:47 PM
     * 修改时间 2019/3/19 4:47 PM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/esignCallback", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String esignCallback(@RequestBody @Valid JSONObject jsonObject, BindingResult br) {
        XbbResponse<BaseVO> response;

        try {

            IOpenAppService iOpenAppService = openAppFactory.openAppFactory(OpenAppEnum.ESIGN);
            iOpenAppService.openAppApiCallbackAccept(jsonObject);
            response = new XbbResponse<>(new BaseVO());
        } catch (XbbException e) {
            e.printStackTrace();
            response = new XbbResponse<>(e);
        }
        return JSON.toJSONString(response);
    }


    /**
     * 云叩回调
     * @param jsonObject
     * @param br
     * @return
     * 创建时间 2019/3/19 4:47 PM
     * 修改时间 2019/3/19 4:47 PM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/cloudCodeCallback", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String cloudCodeCallback(@RequestBody @Valid JSONObject jsonObject, BindingResult br) {
        XbbResponse<BaseVO> response;

        try {

            IOpenAppService iOpenAppService = openAppFactory.openAppFactory(OpenAppEnum.CLOUD_CODE);
            BaseVO baseVO = iOpenAppService.openAppApiCallbackAccept(jsonObject);
            response = new XbbResponse<>(baseVO);
        } catch (XbbException e) {
            e.printStackTrace();
            response = new XbbResponse<>(e);
        }
        return JSON.toJSONString(response);
    }


    /**
     * 钉钉执行动作接收
     * @param jsonObject
     * @param br
     * @return
     */
    @RequestMapping(value = "/ddAction", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String ddAction(@RequestBody @Valid JSONObject jsonObject, BindingResult br) {
        XbbResponse<BaseVO> response;
        try {
            IOpenAppService iOpenAppService = openAppFactory.openAppFactory(OpenAppEnum.DD_CONNECTOR);
            BaseVO baseVO = iOpenAppService.openAppApiCallbackAccept(jsonObject);
            response = new XbbResponse<>(baseVO);
        } catch (XbbException e) {
            e.printStackTrace();
            response = new XbbResponse<>(e);
        }
        return JSON.toJSONString(response);
    }

}
